
/****************************************************************************
**
** Copyright (C) 2004-2008 VATATA.com . All rights reserved.
**
** This file is the part of the Miniutil Poject of VATATA.com.
** Vatata's miniutil library is lightweight and portable (for linux
** and Windows). The C++ Class Library included Threadpool, Dll danymic loading, 
** General socket application model and tcp server, httpserver/servlet, etc. 
** It is a pratical utility disigned for who don't want learn the Big C++ 
** Developing Platform, such as  Boost or ACE.
**
** This file may be used under the terms of the GNU General Public
** License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file.  Alternatively you may (at
** your option) use any later version of the GNU General Public
** License if such license has been publicly approved by VATATA.com.
** (or its successors, if any) and the Miniutil Project Aministrators. 
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://www.gnu.org/licenses/lgpl.html. If you are unsure which 
** license is appropriate for your use, please review the following 
** information: http://code.google.com/p/miniutil/ or contact 
** http://www.vatata.com.
**
** In addition, as a special exception, VATATA.com, as the sole
** copyright holder for Miniutil Project, grants users of VATATA
** P2P Platform the right to release and package with the related 
** libraries without the source code.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. vatata reserves all rights not expressly
** granted herein.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/


#include "miniutil/log.h"
#include "miniutil/thread.h"
#include "miniutil/threadpool.h"
#include "minitest/testcase.h"

#ifdef WIN32
#include "windows.h"
#endif

//CRITICAL_SECTION logmtx;
miniutil::mutex logmtx;

class logcase_1 : public testcase
{
public:

	class LogThreadInstance:public miniutil::runnable
	{
	public:
		LogThreadInstance(int id)
		{
			m_id = id;
			exitflag = false;
		}

		int Start()
		{
			exitflag = false;
			miniutil::threadpool::RunTask( this );
			return 0;
		}
		
		int Stop()
		{
			exitflag = true;
			return 0;
		}

		virtual void run()
		{
			while(true)
			{
				if(exitflag)
					break;
				//EnterCriticalSection(&logmtx);
				logmtx.lock();
				std::cout<<"["<<m_id<<"]";
				//LeaveCriticalSection(&logmtx);
				logmtx.unlock();

				//M_DEBUG("[%d]", m_id);
				miniutil::thread::sleep(rand()/(double)RAND_MAX*1000);
			}
		}

	private:
		int m_id;
		bool exitflag;
	};

	static void* runthread(void* para)
	{
		LogThreadInstance* pins = (LogThreadInstance*)para;
		pins->run();
		return NULL;
	}

	logcase_1()
	{
		//InitializeCriticalSection(&logmtx);
	}

	virtual void test()
	{
		AUTOENTRY;
		LogThreadInstance *plogins[50];
		//spawn 50 thread call M_DEBUG
		{
			AUTOENTRY
		}
		for(int i=0; i< 50; i++)
		{
			plogins[i] = new LogThreadInstance(i);
			plogins[i]->Start();

	//DWORD dwThreadID;
	//HANDLE hvatataThread = NULL; /*receiver thread*/
	//hvatataThread = CreateThread( (LPSECURITY_ATTRIBUTES) NULL, /*security attribute*/
	//			0,/* size of thread stack, same as main thread by default*/
	//			(LPTHREAD_START_ROUTINE)runthread, /* thread entry function */
	//			plogins[i], 0, &dwThreadID);

	//		M_DEBUG("Start %d\n", i);
		}
		miniutil::thread::sleep(1000000);
		for(int i=0; i< 50; i++)
		{
			M_DEBUG("Try Stop %d\n", i);
			plogins[i]->Stop();
			M_DEBUG("Stop %d\n", i);
		}
		return;
	}
};

testcase * t_log_1  = new logcase_1() ;
